package cache

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"log"
	"news001/appconfig"
)

var Rdb *redis.Client
var Ctx = context.Background()

func Init() {
	redisConf := appconfig.AppConf.Redis
	addr := fmt.Sprintf("%s:%d", redisConf.Host, redisConf.Port)
	Rdb = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: "e3e1b140e3741c208ad66c19a21fe8c4", // no password set
		DB:       0,                                  // use default DB
	})

	err := Rdb.Set(Ctx, "key", "value", 0).Err()
	if err != nil {
		panic(err)
	}

	log.Println("redis 连接成功")
}
